% The code is based on the following paper:
%   Chen, T. Y., Lin, Y. L., and Tzeng, L. Y., forthcoming.
%   Estimating probability weighting functions through option pricing bounds. Review of Asset Pricing Studies.
%
% Copyright: Tzu-Ying Chen, Yo-Lan Lin, Larry Y. Tzeng
% Date: January 30, 2024

clear

%% Setting
BV_Target = {'UB'};  

%% Optimal Risk Preferences under Rank-Dependent Expected Utility
FileName = ['Summary_OPT_AllCR_AllPeriod_OP_UB'];
load(FileName, ...
     'Date_Monthly', ...
     'Index_CR', 'Index_OPT', 'OPT_TAB');
clear FileName 

OPT = nan(size(Date_Monthly, 1), length(Index_OPT));                        
for d = 1:size(Date_Monthly, 1)
    OPT(d, :) = OPT_TAB(Index_CR, Index_OPT, d);                                 
end 
clear Index_CR Index_OPT OPT_TAB
clear d
           
%% Panel C: Categorization of Optimal Risk Preferences 
Index_NaN = find(sum(isnan(OPT), 2) > 0);
              
Index_Case1 = find((OPT(:, 1) < 1) & ...
                   (OPT(:, 2) < 1));

Index_Case2 = find((OPT(:, 1) < 1) & ...
                   (OPT(:, 2)==1));

Index_Case3 = find((OPT(:, 1) < 1) & ...
                   (OPT(:, 2) > 1));

Index_Case4 = find((OPT(:, 1)==1) & ...
                   (OPT(:, 2) < 1));

Index_Case5 = find((OPT(:, 1)==1) & ...
                   (OPT(:, 2)==1));

Index_Case6 = find((OPT(:, 1)==1) & ...
                   (OPT(:, 2) > 1));
               
Index_Case7 = find((OPT(:, 1) > 1) & ...
                   (OPT(:, 2) < 1));

Index_Case8 = find((OPT(:, 1) > 1) & ...
                   (OPT(:, 2)==1));

Index_Case9 = find((OPT(:, 1) > 1) & ...
                   (OPT(:, 2) > 1));                
               
TAB_C = [length(Index_Case1) length(Index_Case2) length(Index_Case3) ...
         length(Index_Case4) length(Index_Case5) length(Index_Case6) ...
         length(Index_Case7) length(Index_Case8) length(Index_Case9)];
clear Index_Case1 Index_Case2 Index_Case3 Index_Case4 Index_Case5 Index_Case6 Index_Case7 Index_Case8 Index_Case9 Index_Case10
     
Index_RDEU = find((OPT(:, 1)~=1) | ...
                  (OPT(:, 2)~=1));  
TAB_C(size(TAB_C, 1) + 1, :) = TAB_C / length(Index_RDEU);                
TAB_C(end, 5) = nan;                                                       
clear Index_NaN Index_RDEU

%% Panel D: Summary Statistics (Inverse-Shaped)
Index_RDEU_IS = find(OPT(:, 1) < 1);  
TAB_D = [mean(OPT(Index_RDEU_IS, :), 1); ...
         std(OPT(Index_RDEU_IS, :), [], 1); ...
         quantile(OPT(Index_RDEU_IS, :), [0 0.25 0.50 0.75 1])]';
clear Index_RDEU_IS 
clear Date_Monthly OPT
